#include "mysql.h"
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static MYSQL *conn = NULL;

int init_db() {
    char *server = "localhost";
    char *user = "root";
    char *password = "12345678"; 
    char *database = "device_inspect";

    conn = mysql_init(NULL);

    /* Connect to MySQL server */
    if (!mysql_real_connect(conn, server, user, password, NULL, 0, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        return -1;
    }

    /* Check if database exists */
    if (mysql_query(conn, "SHOW DATABASES LIKE 'device_inspect'")) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        mysql_close(conn);
        conn = NULL;
        return -1;
    }

    MYSQL_RES *res = mysql_store_result(conn);
    if (res && mysql_num_rows(res) == 0) {
        /* Create database if not exists */
        if (mysql_query(conn, "CREATE DATABASE device_inspect")) {
            fprintf(stderr, "%s\n", mysql_error(conn));
            mysql_free_result(res);
            mysql_close(conn);
            conn = NULL;
            return -1;
        }
    }
    mysql_free_result(res);

    /* Select database */
    if (mysql_select_db(conn, database)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        mysql_close(conn);
        conn = NULL;
        return -1;
    }

    /* Create tables if not exists */
    const char *table_creation_queries[] = {
        "CREATE TABLE IF NOT EXISTS cpu_load ("
        "id INT AUTO_INCREMENT PRIMARY KEY, "
        "value TEXT, "
        "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP)",
        
        "CREATE TABLE IF NOT EXISTS memory_user ("
        "id INT AUTO_INCREMENT PRIMARY KEY, "
        "value TEXT, "
        "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP)",
        
        "CREATE TABLE IF NOT EXISTS disk_usage ("
        "id INT AUTO_INCREMENT PRIMARY KEY, "
        "value TEXT, "
        "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP)",
        
        "CREATE TABLE IF NOT EXISTS system_uptime ("
        "id INT AUTO_INCREMENT PRIMARY KEY, "
        "value TEXT, "
        "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"
    };

    for (int i = 0; i < 4; i++) {
        if (mysql_query(conn, table_creation_queries[i])) {
            fprintf(stderr, "%s\n", mysql_error(conn));
            mysql_close(conn);
            conn = NULL;
            return -1;
        }
    }

    return 0;
}

void store_cpu_load(const char* value) {
    if (conn == NULL) {
        fprintf(stderr, "Database connection is not initialized.\n");
        return;
    }

    /* Escape special characters in value */
    char escaped_value[2 * strlen(value) + 1];
    mysql_real_escape_string(conn, escaped_value, value, strlen(value));

    /* Create SQL query */
    char query[1024];
    snprintf(query, sizeof(query), "INSERT INTO cpu_load (value) VALUES ('%s')", escaped_value);

    /* 打印调试信息 */
    printf("Inserting into cpu_load: %s\n", escaped_value);

    /* Send SQL query */
    if (mysql_query(conn, query)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }
}

void store_memory_user(const char* value) {
    if (conn == NULL) {
        fprintf(stderr, "Database connection is not initialized.\n");
        return;
    }

    /* Escape special characters in value */
    char escaped_value[2 * strlen(value) + 1];
    mysql_real_escape_string(conn, escaped_value, value, strlen(value));

    /* Create SQL query */
    char query[1024];
    snprintf(query, sizeof(query), "INSERT INTO memory_user (value) VALUES ('%s')", escaped_value);

    /* 打印调试信息 */
    printf("Inserting into memory_user: %s\n", escaped_value);

    /* Send SQL query */
    if (mysql_query(conn, query)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }
}

void store_disk_usage(const char* value) {
    if (conn == NULL) {
        fprintf(stderr, "Database connection is not initialized.\n");
        return;
    }

    /* Escape special characters in value */
    char escaped_value[2 * strlen(value) + 1];
    mysql_real_escape_string(conn, escaped_value, value, strlen(value));

    /* Create SQL query */
    char query[1024];
    snprintf(query, sizeof(query), "INSERT INTO disk_usage (value) VALUES ('%s')", escaped_value);

    /* 打印调试信息 */
    printf("Inserting into disk_usage: %s\n", escaped_value);

    /* Send SQL query */
    if (mysql_query(conn, query)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }
}

void store_system_uptime(const char* value) {
    if (conn == NULL) {
        fprintf(stderr, "Database connection is not initialized.\n");
        return;
    }

    /* Escape special characters in value */
    char escaped_value[2 * strlen(value) + 1];
    mysql_real_escape_string(conn, escaped_value, value, strlen(value));

    /* Create SQL query */
    char query[1024];
    snprintf(query, sizeof(query), "INSERT INTO system_uptime (value) VALUES ('%s')", escaped_value);

    /* 打印调试信息 */
    printf("Inserting into system_uptime: %s\n", escaped_value);

    /* Send SQL query */
    if (mysql_query(conn, query)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }
}

void uninit_db() {
    if (conn != NULL) {
        mysql_close(conn);
        conn = NULL;
    }
}